package com.zte.smbms_plus.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zte.smbms_plus.pojo.SmbmsBill;
import com.zte.smbms_plus.service.SmbmsBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/bill/")
public class BillController {
    @Autowired
    private SmbmsBillService billService;

    /**
     * 查询所有
     * @param currentPage
     * @param model
     * @return
     */
    @RequestMapping("list/{currentPage}")
    public String list(@PathVariable Long currentPage, Model model) {
        Page<SmbmsBill> smbmsBillPagePage = new Page<>(currentPage, 4);
        billService.page(smbmsBillPagePage);
        model.addAttribute("userPage",smbmsBillPagePage);

        return "billlist";
    }
    //详情查看
    @RequestMapping("details/{id}")
    public String details(@PathVariable("id") Long uid, Model model) {
        SmbmsBill dbill = billService.getById(uid);
        model.addAttribute("dbill",dbill);
        return "billview";
    }

    /**
     * 删除
     * @param uid
     * @param model
     * @return
     */
    @RequestMapping("delete/{id}")
    public String delete(@PathVariable("id") Long uid,Model model){
        boolean flag = billService.removeById(uid);
        if (flag){
            model.addAttribute("msg","删除成功");
        }else {
            model.addAttribute("msg","删除失败");
        }
        return "redirect:/bill/list/1";
    }


    @RequestMapping("goupdata")
    public String goupdata(Long id,Model model){
        SmbmsBill byId = billService.getById(id);
        model.addAttribute("byid",byId);
        return "billmodify";
    }

    @RequestMapping("doupdata")
    public String updateSave(SmbmsBill bill, Model model){
        boolean flag= billService.updateById(bill);
        if (flag){
            return "forward:list/1";
        }else {
            model.addAttribute("修改失败");
            return "forward:list/1";
        }
    }

//    @RequestMapping("add")
//    public String add(){
//        return "static/billadd";
//    }

    /**
     * 添加
     * @param bill
     * @param model
     * @return
     */
    @RequestMapping("save")
    public String save(SmbmsBill bill,Model model){
        boolean flag = billService.save(bill);
        if (flag){
            model.addAttribute("msg","添加成功");
        }else {
            model.addAttribute("msg","添加失败");
        }
        return "forward:list/1";
    }

}
